package com.jar.exercise.坑爹的面试题;

import java.util.Arrays;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 *
 * @author zhangshun
 * @date 2021/5/13 13:28
 */
public class 中位数 {

    public static void main(String[] args) {

        // 需要寻找中位数的数组
        int[] arr = {1,2,3,4,5,6,7,8};

        // 冒泡排序
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length -i -1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        // 如果数组长度是偶数则获取两个数字
        final int length = arr.length;
        if (length % 2 == 0) {
            System.out.println(arr[(arr.length-1) / 2]);
            System.out.println(arr[(arr.length+1) / 2]);
        } else {
            System.out.println(arr[arr.length / 2]);
        }

    }

}
